<?php
$action = $_GET["action"];

$xmlstr = "<crombs></crombs>";
$xml = new SimpleXMLElement($xmlstr);

function createTrack($xml){
  $trackID = uniqid();
  $track = $xml->addChild("track"," ");
  $track->addAttribute("id",$trackID);
  $track->addAttribute("status","CREATED");
  @session_id($trackID);
  @session_start();
}

function deleteTrack($xml){
  $trackID = $_GET["trackid"];
  if (!empty($trackID)){
    $track = $xml->addChild("track"," ");
    $track->addAttribute("id",$trackID);
    $track->addAttribute("status","DELETED");
    @session_id($trackID);
    @session_start();
    @session_destroy();
  }
}

function updatePreyLocation($xml){
  $trackID = $_GET["trackid"];
  $lon = $_GET["lon"];
  $lat = $_GET["lat"];
  $name = $_GET["name"];
  $status = $_GET["status"];
  if (!empty($trackID)){
    @session_id($trackID);
    @session_start();
    if (isset($_SESSION)){
      $_SESSION['longitude']=$lon;
      $_SESSION['latitude']=$lat;
      $_SESSION['name']=$name;
      $_SESSION['status']=$status;
      $track = $xml->addChild("track"," ");
      $track->addAttribute("id",$trackID);
      $track->addAttribute("longitude",$lon);
      $track->addAttribute("latitude",$lat);
      $track->addAttribute("name",$name);
      $track->addAttribute("status",$status);
    }
  }
}
function getPreyLocation($xml){
  $trackID = $_GET["trackid"];
  $lon = $_GET["lon"];
  $lat = $_GET["lat"];
  if (!empty($trackID)){
    @session_id($trackID);
    @session_start();
    if (isset($_SESSION['longitude'])){
      $preyLon = $_SESSION['longitude'];
      $preyLat = $_SESSION['latitude'];
      $name = $_SESSION['name'];
      $status = $_SESSION['status'];
      $track = $xml->addChild("track"," ");
      $track->addAttribute("id",$trackID);
      $track->addAttribute("longitude",$preyLon);
      $track->addAttribute("latitude",$preyLat);
      $track->addAttribute("name",$name);
      $track->addAttribute("status",$status);
      $track->addAttribute("distance",vincentyGreatCircleDistance($lat,$lon,$preyLat,$preyLon));
    } else {
      @session_destroy();
    }

  }
}

function vincentyGreatCircleDistance(
  $latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo, $earthRadius = 6371000)
{
  // convert from degrees to radians
  $latFrom = deg2rad($latitudeFrom);
  $lonFrom = deg2rad($longitudeFrom);
  $latTo = deg2rad($latitudeTo);
  $lonTo = deg2rad($longitudeTo);

  $lonDelta = $lonTo - $lonFrom;
  $a = pow(cos($latTo) * sin($lonDelta), 2) +
    pow(cos($latFrom) * sin($latTo) - sin($latFrom) * cos($latTo) * cos($lonDelta), 2);
  $b = sin($latFrom) * sin($latTo) + cos($latFrom) * cos($latTo) * cos($lonDelta);

  $angle = atan2(sqrt($a), $b);
  return $angle * $earthRadius;
}

switch($action){
  case "create":
    createTrack($xml);
    break;
  case "delete":
    deleteTrack($xml);
    break;
  case "update":
    updatePreyLocation($xml);
    break;
  case "getpreylocation":
    getPreyLocation($xml);
    break;
}

printf($xml->asXML());

?>

